package com.starsea.book.ui.newpage

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityReadHistoryBinding
import com.starsea.book.ui.newpage.adapter.FeedBackListAdapter
import com.starsea.book.ui.newpage.viewmodel.UserViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @createTime 2024-01-10 10 GMT+8
 * @desc : 反馈历史
 */
class FeedBackHistoryActivity : VMBaseActivity<ActivityReadHistoryBinding, UserViewModel>() {

    override val binding by viewBinding(ActivityReadHistoryBinding::inflate)
    override val viewModel by viewModels<UserViewModel>()

    var page = 1

    lateinit var mAdapter: FeedBackListAdapter

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        setTitle(R.string.title_mine_feed_back)

        mAdapter = FeedBackListAdapter(this) {
            startActivity<FeedBackDetailsActivity> { putExtra("provideId", it.provideId) }
        }
        binding.tvEmptyMsg.text = "暂无反馈内容"

        binding.rvContent.layoutManager = LinearLayoutManager(this)
        binding.rvContent.adapter = mAdapter

        binding.refreshLayout.setOnRefreshListener {
            page = 1
            viewModel.getFeedBackList(page)
        }
        binding.refreshLayout.setOnLoadMoreListener {
            page++
            viewModel.getFeedBackList(page)
        }

        viewModel.getFeedBackList(page)

        viewModel.feedBackListResult.observe(this) {
            if (it.status == 200) {

                if (page == 1) {
                    binding.refreshLayout.finishRefresh()
                    mAdapter.setItems(it.data!!.list)
                } else {
                    binding.refreshLayout.finishLoadMore()
                    mAdapter.addItems(it.data!!.list ?: arrayListOf())
                }

                if (it.data!!.list.size < 10) {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData()
                }

                binding.tvEmptyMsg.visible(mAdapter.isEmpty())

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@FeedBackHistoryActivity)
            } else {
                toastOnUi(it.msg)
            }
        }


    }

}